package com.cs157a.view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import com.cs157a.controller.AposApplicationController;

/**
 * The class is the main view of the APOS application.
 * 
 * @author hhong
 * 
 */
public class AposApplicationView extends JFrame {

	private static final long serialVersionUID = 20012100301L;

	private AposApplicationController controller;

	private FilterPanel filterPanel;

	private ContentPanel contentPanel;

	private JSplitPane splitPane;

	private JMenuBar menuBar;
	private JMenu fileMenu;

	private JMenu helpMenu;

	public AposApplicationView(AposApplicationController controller)
			throws HeadlessException {
		super();
		this.controller = controller;

		init();
	}
        
	private void init() {
		initMenuBar();

		filterPanel = new FilterPanel(controller);
		contentPanel = new ContentPanel(controller);

		JScrollPane leftComponent = new JScrollPane(filterPanel);
		// JScrollPane rightComponent = new JScrollPane(contentPanel);

		splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftComponent,
				contentPanel);
		splitPane.setDividerLocation(330);
		splitPane.setOneTouchExpandable(true);
		splitPane.setResizeWeight(0.26);

		Dimension minimumSize = new Dimension(200, 100);
		leftComponent.setMinimumSize(minimumSize);
		contentPanel.setMinimumSize(minimumSize);

		Container container = this.getContentPane();
		container.setLayout(new BorderLayout());
		container.add(menuBar, BorderLayout.NORTH);
		container.add(splitPane, BorderLayout.CENTER);

		this.setPreferredSize(new Dimension(1100, 700));
		this.setTitle(CommonString.APOS);
		this.pack();
		this.setVisible(true);
	}

	private void initMenuBar() {
                
		JMenuItem newAutoMenuItem = new JMenuItem(CommonString.NEW_MAKER);
		newAutoMenuItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// Popup dialog to create new auto.
				controller.newMakerPerformed();
			}

		});

		JMenuItem newAutoModelMenuItem = new JMenuItem(
				CommonString.NEW_AUTO_MODEL);
		newAutoModelMenuItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// Popup dialog to create new auto model.
				controller.newAutoModelPerformed();
			}

		});

		JMenuItem newPartMenuItem = new JMenuItem(CommonString.NEW_PART);
		newPartMenuItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// Popup dialog to create new part.
				controller.newPartPerformed();
			}

		});

		JMenuItem helpMenuItem = new JMenuItem(CommonString.EXIT);
		helpMenuItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// Exit the application gracefully.
				controller.exit();
			}
		});

		fileMenu = new JMenu(CommonString.FILE);
		fileMenu.add(newAutoMenuItem);
		fileMenu.add(newAutoModelMenuItem);
		fileMenu.add(newPartMenuItem);

		fileMenu.add(helpMenuItem);

		helpMenu = new JMenu(CommonString.HELP);

		JMenuItem aboutMenuItem = new JMenuItem(CommonString.ABOUT);
		aboutMenuItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// Popup dialog to display about this application
				// infomatin.
				controller.aboutPerformed();
			}
		});
		helpMenu.add(aboutMenuItem);

		menuBar = new JMenuBar();
		menuBar.add(fileMenu);
		menuBar.add(helpMenu);
	}

	public void cleanUp() {
		filterPanel = null;
		contentPanel = null;
		splitPane = null;
		menuBar = null;
		fileMenu = null;
		helpMenu = null;

		dispose();
	}

        
        
}
